import * as path from "path";
import * as fs from "fs";
import * as globby from 'globby';
import { localLoadFile } from "./file";


export function registerRouter(registerRouter: (controller: any) => void) {
    // 获取controller目录路径
    const baseDir = path.resolve(__dirname, "../controller");
    // 获取所有文件路径
    const filePaths = globby.sync(`**/*.ts`, { cwd: baseDir }); 
    filePaths.forEach((filePath: any) => {
        const fullPath = path.join(baseDir, filePath);

        if (!fs.statSync(fullPath).isFile()) return;
        // 读取文件
        const Controller = localLoadFile(fullPath); 
        if ( Controller?.name) {
            registerRouter(Controller);
        }
    });
};